package br.com.senac.digipao.servlet;

import br.com.senac.digipao.bean.ContasPagar;
import br.com.senac.digipao.dao.ContasDao;
import java.io.IOException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "cadastroContaPagarServlet", urlPatterns = {"/cadastroContaPagarServlet"})
public class CadastroContaPagarServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        
        String operacao = request.getParameter("operacao");
        
        if (operacao.equals("Cadastrar")) {
            // pegando os parâmetros do request
        String descricaoConta = request.getParameter("descricaoConta");
        String dataCadastro = request.getParameter("dataCadastro");
        String dataVencimento = request.getParameter("dataVencimento");
        String dataPagamento = request.getParameter("dataPagamento");

        // monta um objeto contasPagar
        ContasPagar contasPagar = new ContasPagar();
        contasPagar.setDescricaoConta(descricaoConta);
        contasPagar.setDataCadastro(dataCadastro);
        contasPagar.setDataVencimento(dataVencimento);
        contasPagar.setDataPagamento(dataPagamento);

        // salva o cadastro do contasPagar
        try {
            ContasDao dao = new ContasDao();
            dao.adicionaContasPagar(contasPagar);

            RequestDispatcher rd = request.getRequestDispatcher("/media/jsp/cadastradoComSucesso.jsp");
            rd.forward(request, response);

        } catch (SQLException ex) {
            Logger.getLogger(CadastroContaPagarServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
        }
        else if (operacao.equals("Cancelar")) {
            RequestDispatcher rd = request.getRequestDispatcher("/media/jsp/menuPrincipal.jsp");
            rd.forward(request, response);
        }
    }
}
